<?php
namespace Api\Controller;
use Common\Model\UserModel;
use Think\Controller;
use Common\Util\BaseData;

class CommonController extends Controller
{
    protected $user = null;
    protected $utoken = '';
    protected $this_user_id = 0;
    protected $this_user_info = null;
    protected $this_app_config = null;
    protected $this_app_domain;

    public function __construct()
    {
        header('Content-Type:text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*');
        parent::__construct();
        $this->utoken = isset($_GET['utoken']) ? $_GET['utoken'] : (isset($_POST['utoken']) ? $_POST['utoken'] : '');
        $this->this_app_config = BaseData::getAppSetting();
        $this->this_app_domain = '';
    }
    /**
     * 返回封装后的 API 数据到客户端
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return array
     */
    protected function renderJson($code = 0, $msg = '', $data = [])
    {
        return json_encode(array('code' => $code,'data'=>$data,'msg'=>$msg));
    }
    /**
     * 返回操作成功json
     * @param array $data
     * @param string|array $msg
     * @return array
     */
    protected function renderSuccess($data = [], $msg = 'success')
    {
        echo $this->renderJson(1, $msg, $data);exit();
    }
    /**
     * 返回操作失败json
     * @param string $msg
     * @param array $data
     * @return array
     */
    protected function renderError($msg = 'error', $data = [])
    {
        echo $this->renderJson(0, $msg, $data);exit();
    }
    /**
     * 返回操作失败 且返回上一级
     * @param string $msg
     * @param array $data
     * @return array
     */
    protected function renderBackError($msg = 'error', $data = [])
    {
        echo $this->renderJson(201, $msg, $data);exit();
    }
    /**
     * 未登陆返回json
     * @param string $msg
     * @param array $data
     * @return array
     */
    protected function renderNoLogin($msg = 'error', $data = []){
        echo $this->renderJson(101, $msg, $data);exit();
    }

    /**
     * 未授权返回json
     * @param string $msg
     * @param array $data
     */
    protected function renderNoAuth($msg = 'error', $data = []){
        echo $this->renderJson(102, $msg, $data);exit();
    }

    /**
     * 验证用户是否登陆
     * @param $utoken
     * @return bool|mixed
     */
    public function checkUserUtoken($utoken){
        $user_m = new UserModel();
        if(empty($utoken)){
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        $this_user_info = $user_m->getUserInfoByUserToken($utoken);
        if(!$this_user_info) {
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        if($this_user_info['user_token_expires'] <= time()){
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        $this->this_user_id = $this_user_info['id'];
        $this->this_user_info = $this_user_info;
        return $this_user_info;
    }

    //验证用户是否授权登陆
    public function checkUserAuth($utoken){
        $user_m = new UserModel();
        if(empty($utoken)){
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        $this_user_info = $user_m->getUserInfoByUserToken($utoken);
        if($this_user_info == false){
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        if($this_user_info['user_token_expires'] <= time()){
            $this->renderNoLogin('对不起，未登陆或登陆超时');
        }
        if($this_user_info['user_auth_status'] != 2){
            $this->renderNoAuth('对不起，未登陆或登陆超时');
        }
        $this->this_user_id = $this_user_info['id'];
        $this->this_user_info = $this_user_info;
        return $this_user_info;
    }
}
